from nativePlugins.NativePlugin import *
import abc
from abc import *
from network.globalfilter import *
from network.networkfilter import *


def hideElementCSS(element):
    payload = \
        element + " {" \
           "display: none !important;" \
           "visibility: hidden !important;" \
        "}"
    return payload


def hideElementJS(element):
    return "(function () {" + \
           "var node = document.createElement('style');" \
           "node.innerHTML = " + "\"" + hideElementCSS(element) + "\"" + ";" \
           "document.body.appendChild(node);" \
           "delete node;" \
           "})();"

class fuckAdvertisingFilter:
    __metaclass__ = abc.ABCMeta
    enabled = True

    @abstractmethod
    def allowRequest(self, netinfo):
        return True

    @abstractmethod
    def pageLoaded(self, event):
        pass

    @abstractmethod
    def pageLoadStart(self, event):
        pass

    @abstractmethod
    def pageLoadedPercent(self, event, percent):
        pass

    @abstractmethod
    def getName(self):
        return "fuckAdvertising Generic Filter"

class fuckAdvertising(NativePlugin, NetworkFilter):
    faFILTERS = []
    previousFilterName = "fuckAdvertising: Unknown Filter."

    def __init__(self):
        super().__init__("fuckAdvertising.", "com.nulldev.yawk.native.fuckAdvertising", 1.00)

    def onOK(self):
        print("[fuckAdvertising] Running version 1.0.")
        registerFilter(self)
        print("[fuckAdvertising] Loading filters...")
        from nativePlugins.fuckAdvertising.filters.AdGuard.AdGuardFilter import AdGuard_INSTANCE
        self.addFilter(AdGuard_INSTANCE)
        from nativePlugins.fuckAdvertising.filters.fuckAdvertisingSpecific.fuckAdvertisingSpecificFilter import fuckAdvertisingSpecific_INSTANCE
        self.addFilter(fuckAdvertisingSpecific_INSTANCE)
        from nativePlugins.fuckAdvertising.filters.AdsTrackingExtended.AdsTrackingExtendedFilter import AdsTrackingFilter_INSTANCE
        self.addFilter(AdsTrackingFilter_INSTANCE)
        from nativePlugins.fuckAdvertising.filters.pureYouTube.pureYouTubeFilter import PureYouTubeFilter_INSTANCE
        self.addFilter(PureYouTubeFilter_INSTANCE)
        print("[fuckAdvertising] Done.")

    def allowRequest(self, networkRequest):
        try:
            for filter in self.faFILTERS:
                if not filter.allowRequest(networkRequest):
                    self.previousFilterName = filter.getName()
                    return False
            return True
        except Exception as e:
            print(e)

    def addFilter(self, filter):
        if filter is not None:
            self.faFILTERS.append(filter)

    def filterName(self):
        return self.previousFilterName

    def loadEvent(self, event):
        try:
            for filter in self.faFILTERS:
                filter.pageLoaded(event)
        except Exception as e:
            print(e)

    def loadStartEvent(self, event):
        try:
            for filter in self.faFILTERS:
                filter.pageLoadStart(event)
        except Exception as e:
            print(e)

    def loadProgress(self, event, progress):
        try:
            for filter in self.faFILTERS:
                filter.pageLoadedPercent(event, progress)
        except Exception as e:
            print(e)

    def hideElementCSS(self, element):
        payload = element + " {" \
                    "display: none !important;" \
                    "visibility: hidden !important;" \
                  "}"
        return payload

    def hideElementJS(self, element):
        return "var node = document.createElement('style');" \
                  "node.innerHTML = " + "\"" + self.hideElementCSS(element) + "\"" + ";" \
                     "document.body.appendChild(node);" \
                        "delete node;"

fuckAdvertising_INSTANCE = fuckAdvertising()